﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace pgnSoft.Veloz.ERP
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

        void Session_Start(object sender, EventArgs e)
        {

            string[] OriginalUserLanguages = Request.UserLanguages;
            OriginalUserLanguages = new string[] { "en-UK","pt-PT", "it-IT" };
            List<string> CompatibleUserLanguages = new List<string>();
            List<string> CompatibleLanguages = new List<string>(global::Resources.languages.terms.Split(';'));
            
            foreach (string item in OriginalUserLanguages)
            {
                if (CompatibleLanguages.Contains(item))
                {
                    if (!CompatibleUserLanguages.Contains(item))
                    {
                        CompatibleUserLanguages.Add(item);
                    }
                }
            }
            
            CultureInfo ci;
            if (CompatibleUserLanguages.Count() > 0)
            {
                try
                {
                    ci = new CultureInfo(CompatibleUserLanguages[0]);
                    Session["user-culture-default-language"] = ci;
                    Session["user-culture-current-language"] = ci;
                    List<CultureInfo> CultureInfos = new List<CultureInfo>();
                    foreach (string userLanguage in CompatibleUserLanguages)
                    {
                        try
                        {
                            CultureInfos.Add(new CultureInfo(userLanguage));
                        }
                        catch
                        {
                        }
                    }
                    Session["user-culture-languages"] = CultureInfos;
                }
                catch (CultureNotFoundException)
                {
                    ci = CultureInfo.InvariantCulture;
                }
            }
            else if (OriginalUserLanguages.Length > 0)
            {
                foreach (string  OriginalUserLanguage in OriginalUserLanguages)
                {
                    string language = OriginalUserLanguage.Split('-')[0];
                    foreach (string languages in CompatibleLanguages)
                    {
                        if (languages.Substring(0, 2) == language)
                        {
                            ci = new CultureInfo(languages);
                            Session["user-culture-default-language"] = ci;
                            Session["user-culture-current-language"] = ci;
                            Session["user-culture-languages"] = new List<CultureInfo>();
                            return;
                        }
                    }
                }
            }
            else
            {
                ci = CultureInfo.InvariantCulture;
                Session["user-culture-default-language"] = ci;
                Session["user-culture-current-language"] = ci;
                Session["user-culture-languages"] = new List<CultureInfo>();
            }
        }


    }
}